#include "common.h"
int sfd;
Get_service getservice;
user_data data;
//定义两个信号量
sem_t sem_w;
sem_t sem_r;
//用来接收服务器遍历或查找打印出的数据
char find_show_buf[1024];
void *create(void *arg)
{
	int port = (int)arg;
	int slen, cfd;
	int ret;
	struct sockaddr_in serveraddr, clientaddr;
	int alen = sizeof(serveraddr);
	sfd = socket(AF_INET, SOCK_STREAM, 0);
	if (sfd < 0)
	{
		perror("socket");
		return;
	}
	serveraddr.sin_family = AF_INET;
	serveraddr.sin_port = htons(12345);
	serveraddr.sin_addr.s_addr = inet_addr("192.168.1.237");
	ret = connect(sfd, (struct sockaddr *)&serveraddr, alen);
	if (ret < 0)
	{
		perror("connet");
		return;
	}
	getservice.client_key = -1;
	char buf[30];
	printf("#############################\n");
	printf("[1]登入:\n");
	printf("[2]注册:\n");
	while (1)
	{
		switch (getservice.client_key)
		{
			//输入选择登入或是注册
			//一开始服务器就要求输入changge
		case -1:
			if (getservice.wrong == LOGIN_DEFEAT)
			{
				printf("注册失败用户名已存在\n");
				printf("###########################################\n");
				getservice.wrong = 0;
			}
			else if (getservice.wrong == LOGIN_SUCCESS)
			{
				printf("注册并添加用户信息成功\n");
				printf("###########################################\n");
				printf("[1]登入:\n");
				printf("[2]注册:\n");
				getservice.wrong = 0;
			}
			int c;
			scanf("%d", &data.change);
			//解决由于一不小心输入字符串导致的死循环
			// 空循环，读取输入流内的剩余内容
			while ((c = getchar()) != '\n' && c != EOF){
				data.change = 3;
			}
			if (data.change == 1)
			{
				//此时服务端到登入界面
				data.key = 1;
				slen = send(sfd, &data, sizeof(user_data), 0);
			}
			else if (data.change == 2)
			{
				data.key = 2;
				slen = send(sfd, &data, sizeof(user_data), 0);
			}
			else
			{
				data.key = 0;
				slen = send(sfd, &data, sizeof(user_data), 0);
				printf("###########################################\n");
				printf("输入有误请重新输入\n");
				printf("\n");
				printf("###########################################\n");
				printf("[1]登入:\n");
				printf("[2]注册:\n");
			}
			getservice.client_key = -5;
			break;
			//登录界面输入用户密码
		case 1:
			printf("###########################################\n");
			printf("请输入用户名:\n");
			scanf("%s", data.username);
			printf("请输入用户密码:\n");
			scanf("%s", data.password);
			//这时候data.key = 1,2
			//因为在服务端判断密码错误会跳到重新输入登入和注册
			//界面所以这里不用改变data.key的值
			data.key = 1111;
			slen = send(sfd, &data, sizeof(user_data), 0);
			getservice.client_key = -5;
			break;
			//注册界面输入用户密码
		case 2:
			printf("###########################################\n");
			printf("请输入要注册的用户名:\n");
			scanf("%s", data.username);
			printf("请输入要注册用户密码:\n");
			scanf("%s", data.password);
			data.key = 2222;
			slen = send(sfd, &data, sizeof(user_data), 0);
			getservice.client_key = -5;
			break;
			//注册成功
		case LOGIN_SUCCESS:

			printf("注册成功\n");
			printf("###########################################\n");
			printf("请输入电话:\n");
			scanf("%s", data.phone);
			printf("请输入职位:\n");
			scanf("%s", data.work);
			printf("请输入等级:\n");
			scanf("%d", &data.level);
			printf("请输入工资:\n");
			scanf("%d", &data.salary);
			printf("请输入年龄:\n");
			scanf("%d", &data.age);
			data.key = LOGIN_SUCCESS;
			slen = send(sfd, &data, sizeof(user_data), 0);
			getservice.client_key = -5;
			break;
			//要求输入管理员密码
		case 3:
			printf("###########################################\n");
			printf("登入成功\n");
			printf("###########################################\n");
			printf("请输入管理员密码\n");
			scanf("%d", &data.administrator);
			while ((c = getchar()) != '\n' && c != EOF){
				data.administrator = MANAGE_WRONG;
			}
			if (data.administrator == 155)
			{
				//如果管理员密码正服务端进入管理员用户
				data.key = 4;
				slen = send(sfd, &data, sizeof(user_data), 0);
				printf("###########################################\n");
				printf("管理密码正确进入管理用户模式\n");
			}
			else
			{
				//如果管理员密码错误服务端进入普通用户
				data.key = 3;
				slen = send(sfd, &data, sizeof(user_data), 0);
				printf("###########################################\n");
				printf("管理密码错误进入普通用户模式\n");
			}
			getservice.client_key = -5;
			break;

		case 4:
			printf("用户名或密码错误请重新登入\n");
			printf("###########################################\n");
			printf("[1]登入:\n");
			printf("[2]注册:\n");
			getservice.client_key = -1;
			continue;
			break;
		case 11:
			if (getservice.wrong == CHANGE_DEFEAT)
			{
				printf("###########################################\n");
				printf("修改密码失败，请重新选择操作项\n");
				getservice.wrong = 0;
			}
			else if (getservice.wrong == CHANGE_SUCCESS)
			{
				printf("###########################################\n");
				printf("修改密码成功，请重新选择操作项\n");
				getservice.wrong = 0;
			}
			else if (getservice.wrong == FIND_DEFEAT)
			{
				printf("###########################################\n");
				printf("没有查询到用户%s，请重新选择操作项\n", data.username);
				getservice.wrong = 0;
			}
			else if (getservice.wrong == FIND_SUCCESS)
			{
				sem_wait(&sem_r);
				printf("###########################################\n");
				printf("%s\n",find_show_buf);
				getservice.wrong = 0;
				sem_post(&sem_w);
				
			}
			else if (getservice.wrong == SHOW_DEFEAT)
			{
				printf("###########################################\n");
				printf("遍历失败,请重新选择操作项\n");
				getservice.wrong = 0;
			}
			else if (getservice.wrong == SHOW_SUCCESS)
			{
				sem_wait(&sem_r);
				printf("###########################################\n");
				printf("%s\n",find_show_buf);
				getservice.wrong = 0;
				sem_post(&sem_w);
			}
			else
			{
			}
			printf("###########################################\n");
			printf("[1]修改密码\n");
			printf("[2]查询用户\n");
			printf("[3]退出系统\n");
			printf("[4]显示所有用户\n");
			printf("[5]返回上一级\n");
			scanf("%d", &data.change_common);
			if (data.change_common == 1)
			{
				//进入普通用户修改密码页面
				data.key = 11;
				slen = send(sfd, &data, sizeof(user_data), 0);
				printf("请输入新密码\n");
			}
			else if (data.change_common == 2)
			{
				data.key = 12;
				slen = send(sfd, &data, sizeof(user_data), 0);
				printf("请输入要查询的用户名\n");
			}
			else if (data.change_common == 3)
			{
				data.key = 13;
				slen = send(sfd, &data, sizeof(user_data), 0);
				printf("退出成功\n");
			}
			else if (data.change_common == 4)
			{
				data.key = 14;
				slen = send(sfd, &data, sizeof(user_data), 0);
			}
			else if (data.change_common == 5)
			{
				data.key = 15;
				slen = send(sfd, &data, sizeof(user_data), 0);
				printf("###########################################\n");
				printf("[1]登入:\n");
				printf("[2]注册:\n");
			}
			getservice.client_key = -5;
			break;
			//管理密码正确
			//发送给客户端要求客户端接下来输入change_common选项
		case 21:
			if (getservice.wrong == ADD_DEFEAT)
			{
				printf("###########################################\n");
				printf("添加失败用户名重复请重新添加\n");
				getservice.wrong = 0;
				data.key = 21;
				slen = send(sfd, &data, sizeof(user_data), 0);
				break;
			}
			else if (getservice.wrong == ADD_SUCCESS)
			{
				printf("###########################################\n");
				printf("增加用户成功\n");
				getservice.wrong = 0;
			}
			else if (getservice.wrong == DELETE_DEFEAT)
			{
				printf("###########################################\n");
				printf("删除用户失败，请重新选择操作项\n");
				getservice.wrong = 0;
			}
			else if (getservice.wrong == DELETE_SUCCESS)
			{
				printf("###########################################\n");
				printf("删除用户%s成功\n", data.username);
				getservice.wrong = 0;
			}
			else if (getservice.wrong == CHANGE_DEFEAT)
			{
				printf("###########################################\n");
				printf("没查到该用户，请重新选择操作项\n");
				getservice.wrong = 0;
			}
			else if (getservice.wrong == CHANGE_SUCCESS)
			{
				printf("###########################################\n");
				printf("修改成功，请重新选择操作项\n");
				getservice.wrong = 0;
			}
			else if (getservice.wrong == FIND_DEFEAT)
			{
				printf("###########################################\n");
				printf("没有查询到用户%s，请重新选择操作项\n", data.username);
				getservice.wrong = 0;
			}
			else if (getservice.wrong == FIND_SUCCESS)
			{				
				sem_wait(&sem_r);
				printf("###########################################\n");
				printf("%s\n",find_show_buf);
				getservice.wrong = 0;
				sem_post(&sem_w);
				
			}
			else if (getservice.wrong == SHOW_DEFEAT)
			{
				printf("###########################################\n");
				printf("遍历失败,请重新选择操作项\n");
				getservice.wrong = 0;
			}
			else if (getservice.wrong == SHOW_SUCCESS)
			{
				sem_wait(&sem_r);
				printf("###########################################\n");
				printf("%s\n",find_show_buf);
				getservice.wrong = 0;
				sem_post(&sem_w);
			}
			printf("###########################################\n");
			printf("[1]增加用户\n");
			printf("[2]删除用户\n");
			printf("[3]修改用户\n");
			printf("[4]查询用户\n");
			printf("[5]退出系统\n");
			printf("[6]显示所有用户\n");
			printf("[7]返回上一级\n");
			scanf("%d", &data.change_manage);
			if (data.change_manage == 1)
			{
				//管理员界面选择1选项
				data.key = 21;
				slen = send(sfd, &data, sizeof(user_data), 0);

				//管理员界面选择1选项要删除用户
			}
			else if (data.change_manage == 2)
			{
				data.key = 22;
				slen = send(sfd, &data, sizeof(user_data), 0);
				printf("请输入要删除的用户名称\n");
			}
			else if (data.change_manage == 3)
			{
				data.key = 23;
				slen = send(sfd, &data, sizeof(user_data), 0);
			}
			else if (data.change_manage == 4)
			{
				data.key = 24;
				slen = send(sfd, &data, sizeof(user_data), 0);
			}
			else if (data.change_manage == 5)
			{
				data.key = 25;
				slen = send(sfd, &data, sizeof(user_data), 0);
			}
			else if (data.change_manage == 6)
			{
				data.key = 26;
				slen = send(sfd, &data, sizeof(user_data), 0);
			}
			else if (data.change_manage == 7)
			{
				data.key = 27;
				slen = send(sfd, &data, sizeof(user_data), 0);
				printf("###########################################\n");
				printf("		[1]登入:\n");
				printf("		[2]注册:\n");
			}
			else{
				printf("输入有误请重新输入\n");
				getservice.client_key = 21;
				continue;
			}
			getservice.client_key = -5;
			break;
		case 111:
			scanf("%s", data.password);
			//接下来的页面在服务端传过来了
			slen = send(sfd, &data, sizeof(user_data), 0);
			getservice.client_key = -5;
			break;
		case 122:
			scanf("%s", data.username);
			data.key = 122;
			slen = send(sfd, &data, sizeof(user_data), 0);
			getservice.client_key = -5;
			break;
		case 211:
			printf("###########################################\n");
			printf("请输入添加者的用户名\n");
			scanf("%s", data.username);
			printf("请输入添加者的密码\n");
			scanf("%s", data.password);
			printf("请输入添加者的电话:\n");
			scanf("%s", data.phone);
			printf("请输入添加者的职位:\n");
			scanf("%s", data.work);
			printf("请输入添加者的等级:\n");
			scanf("%d", &data.level);
			printf("请输入添加者的工资:\n");
			scanf("%d", &data.salary);
			printf("请输入添加者的年龄:\n");
			scanf("%d", &data.age);
			data.key = 211;
			slen = send(sfd, &data, sizeof(user_data), 0);
			getservice.client_key = -5;
			break;
		case 222: //fwrite(data.username,sizeof(data.username),1,stdin);
			//输入要删除的用户的用户名
			scanf("%s", data.username);
			data.key == 222;
			slen = send(sfd, &data, sizeof(user_data), 0);
			getservice.client_key = -5;
			break;
		case 233:
			printf("###########################################\n");
			printf("请输入需要修改者的用户名:\n");
			scanf("%s", data.username);
			printf("请输入新密码\n");
			scanf("%s", data.password);
			printf("请输入新的电话:\n");
			scanf("%s", data.phone);
			printf("请输入新的职位:\n");
			scanf("%s", data.work);
			printf("请输入新的等级:\n");
			scanf("%d", &data.level);
			printf("请输入新的工资:\n");
			scanf("%d", &data.salary);
			printf("请输入新的年龄:\n");
			scanf("%d", &data.age);
			slen = send(sfd, &data, sizeof(user_data), 0);
			getservice.client_key = -5;
			break;
		case 244: //fwrite(data.username,sizeof(data.username),1,stdin);
			//输入要查询的用户名字
			printf("###########################################\n");
			printf("请输入需要查询的用户名:\n");
			scanf("%s", data.username);
			data.key = 244;
			slen = send(sfd, &data, sizeof(user_data), 0);
			getservice.client_key = -5;
			break;
		case 255:
			/*free(data);
				data = NULL;
				free(getservice);
				getservice = NULL;
				*/
			close(sfd);
			pthread_detach(pthread_self());
			exit(0);
		defaut:
			//getservice.client_key = -5;
			break;
		}
		data.key = getservice.data_key;
	}

	while (1)
	{
		sleep(1);
	}
}
void *read_thread2(void *arg)
{
	//此线程用于接收data_key让客户端每发送一次服务端的data_key变为data.key
	//同时用于接收client_key改变client_key的值
	while (1)
	{
		int ret = recv(sfd, &getservice, sizeof(Get_service), 0);
		if (FIND_SUCCESS == getservice.wrong || SHOW_SUCCESS == getservice.wrong)
		{
			//wait信号量减1p操作
       		//写之前判断能不能写，能就写进去
			sem_wait(&sem_w);
			int ret = recv(sfd, find_show_buf, sizeof(find_show_buf), 0);
      	    //post信号量加1v操作
       	    //写完把1加到读里面去			
			sem_post(&sem_r);			
		}
		if (getservice.client_key == 255)
		{
			pthread_detach(pthread_self());
			exit(0);
		}
	}
	while (1)
	{
		sleep(1);
	}
}
int main(int argc, const char *argv[])
{
	getservice.wrong = 0;
	int port = 12345;
	pthread_t wrtid;
	pthread_t rdtid;
	//0表示线程间通信。后面一个1和0表示信号量初值
    //写的信号量定义为1
    sem_init(&sem_w, 0, 1);
    //读的信号量定义为0
    sem_init(&sem_r, 0, 0);
	pthread_create(&wrtid, NULL, create, (void *)port);
	pthread_create(&rdtid, NULL, read_thread2, NULL);

	while (1)
	{
		sleep(1);
	}
	return 0;
}
